package com.itheima.mapper;

import com.github.pagehelper.Page;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.JobData;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

@Mapper
public interface EmpMapper {

    /**
     * 查询总数
     * @return
     */
    @Select("select count(*) from emp")
    Long count();

    /**
     * 分页查询
     * @param start
     * @param pageSize
     * @return
     */
    @Select("select e.*, d.name deptName from emp e left join dept d on e.dept_id=d.id limit #{start}, #{pageSize}")
    List<Emp> selectPage(@Param("start") Integer start, @Param("pageSize") Integer pageSize);

    @Select("select e.*, d.name deptName from emp e left join dept d on e.dept_id=d.id")
    Page<Emp> list();

    /**
     * 分页条件查询
     * @param empQueryParam
     * @return
     */
    Page<Emp> pageQuery(EmpQueryParam empQueryParam);

    /**
     * useGeneratedKeys: 是否自动生成主键, 配合数据库主键自增auto_increment
     * keyProperty: 指定实体类主键的属性名
     * column: 指定表中主键列的列名
     * @param emp
     * @return
     */
    @Options(useGeneratedKeys = true, keyColumn = "id",keyProperty = "id")
    @Insert("insert into emp (username,password,name,gender,phone,job,salary,image,entry_date,dept_id,create_time,update_time) values(#{username},#{password},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId},#{createTime},#{updateTime})")
    int insert(Emp emp);

    /**
     * 批量删除员工
     * @param ids
     */
    void deleteByIds(List<Integer> ids);

    /**
     * 查询员工基本信息
     * @param id
     * @return
     */
    @Select("select * From emp where id=#{id}")
    Emp selectById(Integer id);

    Emp getById(Integer id);

    /**
     * 更新员工信息
     * @param emp
     */
    Integer updateById(Emp emp);

    /**
     * 员工职位统计
     * @return
     */
    List<JobData> empJobData();

    @MapKey("pos")// 区分唯一的列是哪个,不加也可以的
    List<Map<String, Object>> empJobDataMap();

    /**
     * 按员工性别统计数量
     * @return
     */
    @Select("select if(gender=1,'男','女') name, count(*) value from emp group by gender")
    List<Map<String, Object>> getEmpGenderData();

    /**
     * 查询所有员工
     * @return
     */
    @Select("select * from emp")
    List<Emp> selectAll();

    /**
     * 通过用户名与密码查询用户信息
     * @param emp
     * @return
     */
    @Select("select * from emp where username=#{username} and password=#{password}")
    Emp selectByUsernameAndPassword(Emp emp);
}